#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/wait.h>

extern char** environ;

int main()
{
    pid_t id = fork();

    if(id == 0)
    {
        printf("我是子进程，我的pid : %d\n", getpid());
        
        char* const argv_[] = {
             (char*)"ls",
            (char*)"-l",
            NULL
        };

        execvpe("ls", argv_, environ);

        exit(100); 
    }
    else if(id > 0)
    {
        int status = 0;
        int ret = waitpid(id, &status, 0);
        if(status == 0){
        if(ret > 0) printf("父进程等待成功了！子进程替换成功，退出信号 : %d, 退出码 : %d\n", status&0xF7, (status&0xFF00) >> 8);
        }
        else if(status > 0) 
            printf("父进程等待成功！子进程替换失败，退出信号 : %d, 退出码 : %d\n", status&0xF7, (status&0xFF00) >> 8);
    }

    return 0;
}
